<?php
namespace app\api\controller;

use app\home\model\Article;
use app\home\model\Demand;
use app\home\model\News;
use app\home\model\ShortLink as ModelShortLink;
use app\home\model\Supply;
use think\facade\Db;

class ShortLink
{
    public function index()
    {
        $page = request()->param('page') - 1;
        if ($page < 0)return json([]);
        $page *= 100;
        $sql = "SELECT id FROM stpc_qzz_short_link ORDER BY id DESC LIMIT $page, 1";
        $last_st_id = Db::query($sql)[0]['id'];
        $sts = ModelShortLink::where('id', '<=', $last_st_id)->order('id desc')->limit(0, 100)->select();
        return json([
            'status'=>200,
            'sts'=>$sts,
            'last_id'=>$last_st_id,
            'count'=>ModelShortLink::count(),
        ]);
    }
    public function ping()
    {
        $short_link = request()->param('hash_str');
        $ch = curl_init ();
        curl_setopt($ch, CURLOPT_URL, "http://www.quzizhu.com/?$short_link");
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_NOBODY, FALSE);
        #curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password ); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_exec($ch);
        $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        if ($httpCode === 200)return 1;
        else return 0;
    }
    public function unmarkedLink()
    {
        $news_link = News::field('id')->where('hash_str', '')->select();
        $article_link = Article::field('id')->where('hash_str', '')->select();
        $supply_link = Supply::field('id')->where('hash', '')->select();
        $demand_link = Demand::field('id')->where('hash_str', '')->select();

        $unmarked_link = [
            
        ];

        foreach($news_link as $n_l){
            array_push($unmarked_link,  ['type'=>'新闻', 'id'=>$n_l['id']]);
        };
        foreach($article_link as $a_l){
            array_push($unmarked_link,  ['type'=>'文章', 'id'=>$a_l['id']]);
        };
        foreach($supply_link as $s_l){
            array_push($unmarked_link,  ['type'=>'供应', 'id'=>$s_l['id']]);
        };
        foreach($demand_link as $d_l){
            array_push($unmarked_link,  ['type'=>'需求', 'id'=>$d_l['id']]);
        };

        return json($unmarked_link);
    }
    public function pushLink()
    {
        $data = request()->param();
        $type = $data['type'];
        $option = $data['option'];
        $push_link = [];
        switch($option){
            case '最新':
                $option = true;
                break;
            case '随机':
                $option = false;
        }
        $len = 2000/count($type);
        foreach($type as $t){
            switch($t){
                case "新闻":
                    if ($option){
                        $link = News::field('hash_str')->order('addtime desc')->limit($len)->select();
                        foreach($link as $l){
                            $hash = $l['hash_str'];
                            array_push($push_link, "http://www.quzizhu.com/?$hash");
                        }
                    }else{
                        $sql = "SELECT hash_str FROM stpc_qzz_news WHERE id >= ((SELECT MAX(id) FROM stpc_qzz_news)-(SELECT MIN(id) FROM stpc_qzz_news)) * RAND() + (SELECT MIN(id) FROM stpc_qzz_news)  LIMIT $len";
                        $link = Db::query($sql);
                        foreach($link as $l){
                            $hash = $l['hash_str'];
                            array_push($push_link, "http://www.quzizhu.com/?$hash");
                        }
                    }
                    break;
                case "文章":
                    if ($option){
                        $link = Article::field('hash_str')->order('addtime desc')->limit($len)->select();
                        foreach($link as $l){
                            $hash = $l['hash_str'];
                            array_push($push_link, "http://www.quzizhu.com/?$hash");
                        }
                    }else{
                        $sql = "SELECT hash_str FROM stpc_qzz_article WHERE id >= ((SELECT MAX(id) FROM stpc_qzz_article)-(SELECT MIN(id) FROM stpc_qzz_article)) * RAND() + (SELECT MIN(id) FROM stpc_qzz_news)  LIMIT $len";
                        $link = Db::query($sql);
                        foreach($link as $l){
                            $hash = $l['hash_str'];
                            array_push($push_link, "http://www.quzizhu.com/?$hash");
                        }
                    }
                    break;
            }
        }
        return json($push_link);
    }
}
